package com.atguigu.es.demo;

import io.searchbox.client.JestClient;
import io.searchbox.client.JestClientFactory;
import io.searchbox.client.config.HttpClientConfig;
import io.searchbox.core.DocumentResult;
import io.searchbox.core.Index;

import java.io.IOException;

/**
 * Created by Smexy on 2021/7/9
 *
 *     写操作
 *          删除和插入
 *
 *

 */
public class WriteDemo1 {

    public static void main(String[] args) throws IOException {

        //创建一个 JestClientFactory
        JestClientFactory jestClientFactory = new JestClientFactory();

        HttpClientConfig httpClientConfig = new HttpClientConfig.Builder("http://hadoop102:9200").build();

        jestClientFactory.setHttpClientConfig(httpClientConfig);

        // 从工厂中获取一个 客户端对象
        JestClient jestClient = jestClientFactory.getObject();

        String indexStr="{\n" +
                "        \"empid\" : 1021,\n" +
                "          \"age\" : 42,\n" +
                "          \"balance\" : 3400,\n" +
                "          \"name\" : \"李三\",\n" +
                "          \"gender\" : \"男\",\n" +
                "          \"hobby\" : \"逛酒吧,购物\"\n" +
                "}";

        ///test/emps/21
        Index index = new Index.Builder(indexStr)
                .type("emps")
                .index("test")
                .id("22")
                .build();

        DocumentResult execute = jestClient.execute(index);

        System.out.println(execute.getResponseCode());

        System.out.println(execute.getErrorMessage());


        //关闭客户端
        jestClient.close();


    }
}
